package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import algri.AnaData;
import db.Conn_DB;


/**
 * Servlet implementation class Req_HappenLine
 */
@WebServlet("/Req_HappenLine")
public class Req_HappenLine extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Req_HappenLine() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//需要给前端提供诸如这样的json数据：{('time':'2020-05-21','happen_times':'3')}
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out=response.getWriter();
				//获取前端数据 spot_name sel_year sel_month
				String spot_nameString=request.getParameter("spot_name");
				String load_start_time=request.getParameter("load_start_time");
				String load_end_time=request.getParameter("load_end_time");
				//数据库连接池
				Conn_DB connectDB=new Conn_DB();
				//调用数据分析类
				AnaData analyzedata=new AnaData();
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setHeader("Access-Control-Allow-Methods", "Get,Post");
				List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
				List<HashMap<String, String>> restList=new ArrayList<HashMap<String,String>>();
				//Gson转换json工具
				Gson gson=new Gson();
				//list为数据库全部结果容器，准备下一步传入analyze各个方法进行进一步分析处理
				list=connectDB.returnResult(spot_nameString,load_start_time,load_end_time);
//				list=connectDB.returnResult("hyks", "2020-02-08", "2020-04-05");
				restList.clear();
				restList=analyzedata.ana_happen_line(list);
				String json=gson.toJson(restList);
//				System.out.println("事件发生次数折线图发生次数json打印数据："+json);
				out.write(json);
				out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
